package com.forevershine.action;

import static com.forevershine.common.util.DateUtils.convertStringToDate;
import static com.forevershine.common.util.FileUtils.hasText;
import static com.forevershine.common.util.GsonConverter.fromObj;
import static com.forevershine.common.util.HttpRequestResponseHelper.getParam;
import static com.forevershine.common.util.HttpRequestResponseHelper.getSessionValue;
import static com.forevershine.common.util.HttpRequestResponseHelper.writeJsonResponse;
import static com.forevershine.common.util.HttpRequestResponseHelper.downloadFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import org.apache.struts2.ServletActionContext;

import com.forevershine.business.context.ContextBuilder;
import com.forevershine.business.report.PDFReportExporter;
import com.forevershine.business.report.model.SampleNoteReport;
import com.forevershine.business.service.impl.SampleNoteReportServiceImpl;
import com.forevershine.common.BusinessConstants;
import com.opensymphony.xwork2.ActionSupport;

public class ReportAction extends ActionSupport {

	private static final long serialVersionUID = 331267719504590L;
	private static final String REPORT_PREFIX = "report/";
	private static SimpleDateFormat sdFormat = new SimpleDateFormat(
			"yyyyMMddHHmmss");

	public void prepareReport() {
		// enhance security by verify user login and before generate report;
		int taskid = tryToGetTaskId();

		HashMap<String, String> responseMap = new HashMap<String, String>();

		if (taskid <= 0) {
			responseMap.put("error", "true");
			writeJsonResponse(fromObj(responseMap));
			return;
		}

		SampleNoteReport report = updateReportByTaskId(taskid);

		// TODO direct return the url path inside server is not safe, when we
		// expose site to external clients.
		String pathToDownload = ServletActionContext.getServletContext().getRealPath("")+"/"+REPORT_PREFIX+"samplereport-" + report.getIndentno()
				+"-"+ sdFormat.format(report.getIssuedate())+".pdf";
		try {
			PDFReportExporter.export(BusinessConstants.SAMPLE_REPORT_TEMPLATE,
					pathToDownload, report);
		} catch (IOException e) {
			e.printStackTrace();
			responseMap.put("error", "true");

		}
		responseMap.put("url", "downloadReport.do?filename="+"samplereport-" + report.getIndentno()
				+"-"+ sdFormat.format(report.getIssuedate())+".pdf");
		writeJsonResponse(fromObj(responseMap));
	}
	
	public void downloadReport()
	{
		if (hasText(getParam("filename")))
		{
			String fullPath = ServletActionContext.getServletContext().getRealPath("")+"/"+REPORT_PREFIX +getParam("filename");
			downloadFile(fullPath);
		}
		
	}
	

	private int tryToGetTaskId() {
		Integer taskid = -1;
		if (hasText(getParam(BusinessConstants.TASKID))) {
			taskid = Integer.valueOf(getParam(BusinessConstants.TASKID));
		} else {
			if (hasText(getSessionValue(BusinessConstants.TASKID))) {
				taskid = Integer
						.valueOf(getSessionValue(BusinessConstants.TASKID));
			}
		}
		return taskid;
	}

	private SampleNoteReport updateReportByTaskId(int taskid) {
		SampleNoteReportServiceImpl sampleNoteReportSvc = ContextBuilder
				.getContext().getInstance(SampleNoteReportServiceImpl.class);

		SampleNoteReport report = sampleNoteReportSvc
				.loadSampleNoteReport(taskid);

		report.setIssuedate(new Date());
		try {
			report.setOrder_duedate(convertStringToDate(
					BusinessConstants.UI_DATE_PATTERN,
					getParam("order_duedate")));

			report.setSample_duedate(convertStringToDate(
					BusinessConstants.UI_DATE_PATTERN,
					getParam("sample_duedate")));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		report.setTargetprice(Float.valueOf(getParam("targetprice")));
		report.setSample_units(Integer.valueOf(getParam("sample_units")));

		sampleNoteReportSvc.updateSampleNoteReport(report);

		return report;
	}

}
